package com.hanlink.dsp.ca.model.channel;

import com.hanlink.dsp.protocols.XoverProtocol;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import java.io.Serializable;

import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_BAND_BPASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_BAND_PASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_HIGH_BYPASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_HIGH_PASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_LOW_BYPASS;
import static com.hanlink.dsp.protocols.XoverProtocol.PASS_TYPE_LOW_PASS;
import static com.hanlink.dsp.protocols.XoverProtocol.XOVER_FILTER_TYPE_BUTTER_W;

@Table(name = "_xover_channels")
public class XoverChannel implements Serializable {

    @Column(name = "id", isId = true)
    private int id;
    @Column(name = "settings_id")
    private int settingsId; //所属设置ID

    @Column(name = "onOff")
    private int onOff; //打开关闭

    @Column(name = "channel" )
    private int channel; //1 ,2 ,3 ,4,5

    @Column(name = "_pass_type")
    private int passType = 1; //  1 高通 2 低通 3高通bypass 4 低通bypass   5 bandpass

    @Column(name = "high_filter_type")
    private int highFilterType = XOVER_FILTER_TYPE_BUTTER_W; //Butter-W
    @Column(name = "high_filter_value")
    private int highFilterValue = 0; //默认0  12db
    @Column(name = "high_frenquency")
    private int highFrequency = 20;


    @Column(name = "low_filter_type")
    private int lowFilterType = XOVER_FILTER_TYPE_BUTTER_W;
    @Column(name = "low_filter_value")
    private int lowFilterValue = 0;
    @Column(name = "low_frenquency")
    private int lowFrequency = 20000;



    public XoverChannel() {
        super();
    }

    public XoverChannel(int channel) {
        super();
        this.channel = channel;
    }
    public void resetData(){
        passType = 0;
        onOff = 1;
        resetHighPass();
        resetLowPass();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getHighFilterType() {
        return highFilterType;
    }

    public void setHighFilterType(int highFilterType) {
        this.highFilterType = highFilterType;
    }

    public int getHighFilterValue() {
        return highFilterValue;
    }

    public void setHighFilterValue(int highFilterValue) {
        this.highFilterValue = highFilterValue;
    }

    public int getHighFrequency() {
        return highFrequency;
    }

    public void setHighFrequency(int highFrequency) {
        this.highFrequency = highFrequency;
    }

    public int getLowFilterType() {
        return lowFilterType;
    }

    public void setLowFilterType(int lowFilterType) {
        this.lowFilterType = lowFilterType;
    }

    public int getLowFilterValue() {
        return lowFilterValue;
    }

    public void setLowFilterValue(int lowFilterValue) {
        this.lowFilterValue = lowFilterValue;
    }


    public void setHighPassValue(int filterType, int value, int frenquency) {
        setHighFilterType(filterType);
        setHighFilterValue(value);
        setHighFrequency(frenquency);
    }

    public void setLowPassValue(int filterType, int value, int frenquency) {
        setLowFilterType(filterType);
        setLowFilterValue(value);
        setLowFrequency(frenquency);
    }

    public void syncGroupData(XoverChannel xoverChannel) {
        setHighPassValue(xoverChannel.getHighFilterType(), xoverChannel.getHighFilterValue(), xoverChannel.getHighFrequency());
        setLowPassValue(xoverChannel.getLowFilterType(), xoverChannel.getLowFilterValue(), xoverChannel.getLowFrequency());
    }

    public int getLowFrequency() {
        return lowFrequency;
    }

    public void setLowFrequency(int lowFrequency) {
        this.lowFrequency = lowFrequency;
    }

    public int getSettingsId() {
        return settingsId;
    }

    public void setSettingsId(int settingsId) {
        this.settingsId = settingsId;
    }

    public int getOnOff() {
        return onOff;
    }

    public void setOnOff(int onOff) {
        this.onOff = onOff;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    @Override
    public String toString() {
        return "XoverChannel{" +
                "id=" + id +
                ", onOff=" + onOff +
                ", passType=" + passType +
                ", channel=" + channel +
                ", highFilterType=" + highFilterType +
                ", highFilterValue=" + highFilterValue +
                ", highFrequency=" + highFrequency +
                ", lowFilterType=" + lowFilterType +
                ", lowFilterValue=" + lowFilterValue +
                ", lowFrequency=" + lowFrequency +
                ", settingsId=" + settingsId +
                '}';
    }

    public int getPassType() {
        return passType;
    }

    public void setPassType(int passType) {
        this.passType = passType;
    }

    public void sendCmd(int passType) {
        switch (passType){
            case PASS_TYPE_BAND_PASS:
                XoverProtocol.send(this, PASS_TYPE_LOW_PASS, true);
                XoverProtocol.send(this, PASS_TYPE_HIGH_PASS, true);
                break;
            case PASS_TYPE_BAND_BPASS:
                XoverProtocol.send(this, PASS_TYPE_HIGH_BYPASS, true);
                XoverProtocol.send(this, PASS_TYPE_LOW_BYPASS, true);
                break;
            default:
                XoverProtocol.send(this, passType, true);
                break;
        }

       // XoverProtocol.send(this, getPassType(), true);
    }
    public void sendCmd() {
        sendCmd(getPassType());
//        XoverProtocol.send(this, getPassType(), true);
    }


    public void resetHighPass() {
        highFilterType = 1;
        highFilterValue = 0;
        highFrequency = 20;
    }

    public void resetLowPass() {
        lowFilterType = 1;
        lowFilterValue = 0;
        lowFrequency = 20000;
    }

    public XoverChannel copy() {
        final XoverChannel xc = new XoverChannel();
        xc.id = id;
        xc.settingsId = settingsId;
        xc.onOff = onOff;
        xc.channel = channel;
        xc.passType = passType;
        xc.highFilterType = highFilterType;
        xc.highFilterValue = highFilterValue;
        xc.highFrequency = highFrequency;
        xc.lowFilterType = lowFilterType;
        xc.lowFilterValue = lowFilterValue;
        xc.lowFrequency = lowFrequency;

        return xc;
    }
}
